package homework;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

/**
 * @author mubei
 * @date 2021/12/2
 * @description
 */
public class Demo05 {

    public static void main(String[] args) {
        ArrayList<PCA> list = new ArrayList<>();

        list.add(new PCA("ah1", 10, 10));
        list.add(new PCA("ah2", 5, 10));
        list.add(new PCA("ah3", 6, 5));
        list.add(new PCA("ah4", 1, 6));
        Collections.sort(list);

        int count = 0;
        while (list.size() != 0) {
            PCA pca = list.get(0);
            list.remove(0);
            pca.cpuTime += 1;
            System.out.println("第"+(count++)+"cpu时间  进程"+pca.name+"  运行了:"+pca.cpuTime);
            if(pca.cpuTime != pca.runTIme){
                pca.priority -= 1;
                list.add(pca);
                Collections.sort(list);
            }else{
                System.out.println("恭喜恭喜    "+pca.name+"    运行结束");
            }
        }
    }
}

enum ThreadState {
    W, R, F;
}

class PCA implements Comparable {
    String name;
    int priority;
    long arriveTime;
    int runTIme;
    int cpuTime;
    ThreadState state;

    /*
     * 线程名称
     * 优先级
     * 需要时间
     * cpu时间
     * */
    public PCA(String name, int priority, int runTIme) {
        this.name = name;
        this.priority = priority;
        this.arriveTime = System.currentTimeMillis();
        this.runTIme = runTIme;
        this.cpuTime = 0;
        this.state = ThreadState.W;
    }

    @Override
    public int compareTo(Object o) {
        PCA b = (PCA) o;
        if (b.priority == this.priority) {
            return Integer.compare((this.runTIme - this.cpuTime), (b.runTIme - b.cpuTime));
        } else {
            return Integer.compare(b.priority, this.priority);
        }

    }
}
